package com.tmpt.utils.rsa;

import java.io.InputStream;
import java.security.NoSuchAlgorithmException;

import com.tmpt.utils.MD5Util;



public class SignUtils {
	
	public static void main(String[] args) {
		try {
			String plaintext = "";
			String ciphertext = "";
			//decode
			if (args[0].equals("-d") && args.length == 3) {
				if (args[1].equals("-text")) {
					plaintext = args[2];
					plaintext = SignUtils.decodeByPublic(plaintext);
					//.out.println(plaintext);
				} else{
					//.out.println("command err1");
				}
			} 
			//verify
			else if (args[0].equals("-v") && args.length == 5) {
				if (args[1].equals("-text")) {
					plaintext = args[2];
					if (args[3].equals("-sign")) {
						ciphertext = args[4];
						boolean result = SignUtils.verify(plaintext, ciphertext);
						//.out.println(result);
					} else{
						//.out.println("command err2");
					}
				} else{
					//.out.println("command err3");
				}
			} else{
				//.out.println("command err4");
			}
		} catch (Exception e) {
			e.printStackTrace();
			//.out.println("command err5");
		}
	}
	
	
	public static void main1(String[] args) throws NoSuchAlgorithmException{
		String m = "431AAE2D770AD79F4B3D478E5B48E2989B2A29C4FADCDD47D8233339631139389573CEA9CB76C4D2420E4E5AC9766973FF4B75439D82DEBC94E75DB36730FDF4A65EBEA530F0575EF2C0EECE68A74350B81BEDD216B47E931E00223D8B5852139E9198443F5B11F8FCD7339999D66D3F9FBF582363E67EDD459B22252B86497F";
		//String s = SignUtils.decodeByPublic(m);
		////.out.println(s);
		boolean result = SignUtils.verify("0123456789",m);
		//.out.println(result);
	}
	
	private static String publicKeyPath = "/20120830.public";
	
	/**
	 * 公钥解密，解密后的结果是MD5
	 * @param miByPrivateKey:经私钥加密的密文，及签名
	 * @return
	 */
	public static String decodeByPublic(String miByPrivateKey){
		String mingHex = "";
	    try {
	    	InputStream keyStream = SignUtils.class.getResourceAsStream(SignUtils.publicKeyPath);
	    	EncodeAction encodeAction = new EncodeAction(keyStream);
	    	mingHex = encodeAction.decodeDataByPublic(miByPrivateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mingHex;
	}

	/**
	 * 校验
	 * @param mingMsg:拼接的明文
	 * @param miByPrivateKey:经私钥加密的密文，及签名(MD5)
	 * @return 相等返回true, 否则false
	 * @throws NoSuchAlgorithmException 
	 */
	public static boolean verify(String mingMsg, String miByPrivateKey) throws NoSuchAlgorithmException{
		String mingDecodeMD5 = SignUtils.decodeByPublic(miByPrivateKey);
		String mingMsgMD5 = MD5Util.toMD5(mingMsg);
		
		if(mingMsgMD5.equals(mingDecodeMD5)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 校验
	 * @param mingMsg:拼接的明文
	 * @param miByPrivateKey:经私钥加密的密文，及签名(MD5)
	 * @param publicKeyPath:公钥路径
	 * @return 相等返回true, 否则false
	 * @throws NoSuchAlgorithmException 
	 */
	public static boolean verify(String mingMsg, String miByPrivateKey, String publicKeyPath) throws NoSuchAlgorithmException{
		SignUtils.publicKeyPath = publicKeyPath;
		String mingDecodeMD5 = SignUtils.decodeByPublic(miByPrivateKey);
		String mingMsgMD5 = MD5Util.toMD5(mingMsg);
		
		if(mingMsgMD5.equals(mingDecodeMD5)){
			return true;
		}else{
			return false;
		}
	}
}
